﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Linq;
using LogLib;
using System.IO;
using System.Reflection;

namespace HZWebService
{
    public class HzConfigTriples
    {
        public string Key = "";

        public string Name = "";

        public string Url = "";

        public bool NeedLogin = false;

        public HzConfigTriples()
        {

        }

        public HzConfigTriples(string key, string name, string url, bool login)
        {
            Key = key;
            Name = name;
            Url = url;
            NeedLogin = login;
        }   
    }

    public static class WebSetting
    {
        #region static Fields and Functions

        public static string CurrentDirectory = "";

        private static string _strIniFileName = @"..\WSClient.ini";

        public static string IniFileName
        {
            get { return _strIniFileName; }
        }

        public static Dictionary<string, string> DicAisinoAdress = new Dictionary<string, string>();

        private static string SectionName = "ClientConfig";

        static WebSetting()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            CurrentDirectory = directory.Substring(6);
            _strIniFileName = Path.Combine(CurrentDirectory, "Config", "WSClient.ini");
            ReadAllAdress();
        }

        public static long WriteValue(string section, string key, object value)
        {
            long i = WritePrivateProfileString(section, key, value.ToString(), _strIniFileName);
            return i;
        }

        public static bool LoadBoolenValue(string field)
        {
            bool val = Convert.ToBoolean(IniValue("PeopleCheck", field, _strIniFileName));
            return val;
        }

        public static string LoadStringValue(string field)
        {
            return IniValue(SectionName, field, _strIniFileName);
        }

        #region 读取INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey, string iniFileName)
        {   
            StringBuilder temp = new StringBuilder(500);
            //MessageBox.Show(iniFileName);
            int i = GetPrivateProfileString(Section, IniKey, "", temp, 500, iniFileName);
            return temp.ToString();
        }

        /// <summary>
        /// 读取INI文件(找不到配置文件返回默认值)
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static string IniValue(string Section, string IniKey, string strDefault, string iniFileName)
        {
            StringBuilder temp = new StringBuilder(500);
            //MessageBox.Show(iniFileName);
            int i = GetPrivateProfileString(Section, IniKey, strDefault, temp, 500, iniFileName);
            return temp.ToString();
        }

        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion

        #region 写INI文件

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section">段落</param>
        /// <param name="IniKey">关键字</param>
        /// <returns></returns>
        public static long WriteValue(string Section, string IniKey, string NewValue, string iniFileName)
        {
            long i = WritePrivateProfileString(Section, IniKey, NewValue, iniFileName);
            return i;
        }

        [DllImport("kernel32")]
        public static extern long WritePrivateProfileString(
            string section, string key, string val, string filePath);
        #endregion

        private static void ReadAllAdress()
        {            
            try
            {
                DicAisinoAdress.Clear();
                string path = Path.Combine(CurrentDirectory, "Config","WebAdressList.xml");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(path);
                XmlElement root = xmlDoc["adress"];          
                XmlElement aisinoNode = root["Aisino"];         
                XmlNodeList rowListAisino = aisinoNode.ChildNodes;
                string url, key;
                for (int j = 0; j < rowListAisino.Count; j++)
                {
                    XmlElement column = (XmlElement)rowListAisino[j];
                    key = column.Attributes["key"].Value;               
                    url = column.Attributes["value"].Value;
                    DicAisinoAdress.Add(key, url);
                }
            }
            catch(Exception ex)
            {
                Log.WriteException(ex.ToString());
                throw ex;
            }
        }
        #endregion

    }
}
